package com.cspm.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据库连接池配置
 * Created by CHQIU on 2017-07-31.
 */
@Configuration
public class DruidConfig {
    /**
     * ServletRegistrationBean,
     *
     * @return
     * @see com.alibaba.druid.support.http.ResourceServlet
     */
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean druid = new ServletRegistrationBean();
        druid.setServlet(new StatViewServlet());
        druid.setUrlMappings(Arrays.asList("/admin/druid/*"));
        Map<String, String> params = new HashMap<>();
        params.put("loginUsername", "admin");
        params.put("loginPassword", "admin");
        //是否能够重置数据
        params.put("resetEnable", "true");
        druid.setInitParameters(params);
        return druid;
    }

    /**
     * @return
     * @see com.alibaba.druid.support.http.WebStatFilter
     */
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean fitler = new FilterRegistrationBean();
        fitler.setFilter(new WebStatFilter());
        fitler.setUrlPatterns(Arrays.asList("/*"));
        fitler.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/admin/druid/*,/u/user/logoutAjax,/u/user/logout");
        fitler.addInitParameter("sessionStatMaxCount", "2000");
        fitler.addInitParameter("sessionStatEnable", "true");
        fitler.addInitParameter("principalSessionName", "SESSION_AHJYBC_USERID");
        fitler.addInitParameter("profileEnable", "true");
        return fitler;
    }
}
